<template>
  <div style="height: 65px">
    <TopMenu :disabled-count="false" :disabled-trade="false" :disabled-message="false"></TopMenu>
  </div>
  <div>
    <div class="background_div">碳核算记录查询</div>
  </div>
  <div>
    <el-form :inline="true" :model="queryParams" style="margin-top: 20px; height: 33px">
      <el-form-item label="公司名称：" style="margin-left: 130px; font-weight: bold">
        <el-input v-model="queryParams.companyName" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="提交时间：" style="margin-left: 170px; font-weight: bold">
        <el-input v-model="queryParams.submissionTime" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-main>
      <el-scrollbar>
        <el-table :data="tableData" stripe style="width: 100%" max-height="300">
          <el-table-column fixed type="index" label="序号" width="100" align="center" />
          <el-table-column prop="companyName" label="公司名称" width="300" align="center" />
          <el-table-column prop="updateTime" label="提交时间" width="300" align="center">
            <template #default="scope">
              <div style="display: flex; align-items: center; margin-left: 30%">
                <span style="margin-left: 10px">{{
                  moment(scope.row.updateTime).format('YYYY-MM-DD  h:mm')
                }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="碳核算数据" width="250" align="center">
            <template #default="scope">
              <el-button size="small" type="primary" @click="handlePrimary(scope.row)"
                ><a :href="scope.row.url">下载</a></el-button
              >
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template #default="scope">
              <el-button size="small" type="success" @click="handleSuccess(scope.$index, scope.row)"
                >通过
              </el-button>
              <el-button size="small" type="danger" @click="handleDanger(scope.row)"
                >打回
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-scrollbar>
    </el-main>
  </div>
  <div>
    <el-pagination
      background
      layout="prev, pager, next"
      :page-count="totalPage"
      @change="doPageChange"
    />
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import TopMenu from '@/components/Menu/TopMenu.vue'
import {
  CompanyControllerService,
  DataReviewControllerService,
  type DataReviewUpdateRequest
} from '../../../../generated'
import moment from 'moment'
import { ElMessage } from 'element-plus'

const totalPage = ref(10)

interface Data {
  companyName: ''
}

const tableData = ref<Data>()

// 查询参数
const queryParams = ref({
  companyName: '',
  submissionTime: '',
  current: 0,
  pageSize: 10,
  ossStatus: 0
})
const loadData = async () => {
  const res = await DataReviewControllerService.listMyDataReviewVoByPageUsingPost(queryParams.value)
  tableData.value = res.data.records
  console.log(tableData.value)
  for (let i in tableData.value) {
    const res2 = await CompanyControllerService.getByIdUsingPost(tableData.value[i].companyId)
    tableData.value[i].companyName = res2.data.companyName
  }
  totalPage.value = Math.floor(
    (Number(queryParams.value.pageSize - 1) + Number(res.data.total)) /
      Number(queryParams.value.pageSize)
  )
}
onMounted(() => {
  loadData()
})
/**
 * 分页请求
 * @param page
 */
const doPageChange = (page: any) => {
  queryParams.value = page
  loadData()
}

function onSubmit() {}

/**
 * 下载
 * @param res
 */
function handlePrimary(res: any) {}

/**
 * 通过
 * @param res
 */
const handleSuccess = async (index: any, res: any) => {
  const data = ref<DataReviewUpdateRequest>({
    id: res.id,
    ossStatus: 1
  })

  const res2 = await DataReviewControllerService.updateByIdUsingPost(data.value)

  if (res2.code == 0) {
    ElMessage({
      message: '数据审核通过',
      type: 'success'
    })
    //todo 刷新
  } else {
    ElMessage({
      message: '数据审核不通过-数据库异常',
      type: 'error'
    })
  }
}

/**
 * 打回
 * @param res
 */
const handleDanger = async (res: any) => {
  const data = ref<DataReviewUpdateRequest>({
    id: res.id,
    isDelete: 1
  })

  const res2 = await DataReviewControllerService.updateByIdUsingPost(data.value)

  if (res2.code == 0) {
    ElMessage({
      message: '数据审核不通过-打回成功',
      type: 'success'
    })
  } else {
    ElMessage({
      message: '数据审核不通过-打回失败-数据库异常',
      type: 'error'
    })
  }
}
</script>

<style scoped>
.background_div {
  background-color: #409eff;
  padding: 10px;
  margin: 10px;
}

.pagination-style {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
